package com.ybf.user.dal.manager;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ybf.user.dal.entity.AdminDO;
import com.ybf.user.dal.entity.query.AdminQueryDO;
import com.ybf.user.dal.mapper.AdminMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.Objects;

/**
 * @Date
 * @Author
 **/
@Component
public class AdminManager extends ServiceImpl<AdminMapper, AdminDO> {

    @Autowired
    private AdminMapper adminMapper;

    /**
     * 获取信息 - 机构id - 手机号
     *
     * @author 向南
     * @date 14:37 2023/7/7
     * @param: orgId
     * @param: mobile
     **/
    public AdminDO selectByMobile(String mobile) {
        if (StringUtils.isEmpty(mobile)) {
            return null;
        }
        return adminMapper.selectOne(new LambdaUpdateWrapper<AdminDO>()
                .eq(AdminDO::getMobile, mobile));
    }

    /**
     *
     *
     * @author 向南
     * @date 14:54 2023/7/7
     * @param: id
     **/
    public AdminDO selectById(Long id) {
        if (Objects.isNull(id)) {
            return null;
        }
        return adminMapper.selectById(id);
    }

    /**
     * 列表 - 分页
     *
     * @author 向南
     * @date 15:30 2023/7/7
     * @param: queryDO
     **/
    public IPage<AdminDO> selectPage(AdminQueryDO queryDO) {
        if (Objects.isNull(queryDO)) {
            return null;
        }
        LambdaQueryWrapper<AdminDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AdminDO::getOrgId, queryDO.getOrgId());
        if (!CollectionUtils.isEmpty(queryDO.getIds())) {
            queryWrapper.in(AdminDO::getId, queryDO.getIds());
        }
        if (!StringUtils.isEmpty(queryDO.getName())) {
            queryWrapper.like(AdminDO::getName, queryDO.getName());
        }
        if (!StringUtils.isEmpty(queryDO.getMobile())) {
            queryWrapper.like(AdminDO::getMobile, queryDO.getMobile());
        }
        if (!StringUtils.isEmpty(queryDO.getStartTime())) {
            queryWrapper.ge(AdminDO::getGmtCreate, queryDO.getStartTime());
        }
        if (!StringUtils.isEmpty(queryDO.getEndTime())) {
            queryWrapper.le(AdminDO::getGmtCreate, queryDO.getEndTime());
        }
        return adminMapper.selectPage(queryDO.getPage(), queryWrapper);
    }
}
